package com.dcloud.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcloud.enums.BizErrorEnum;
import com.dcloud.exception.BizException;
import com.dcloud.manage.mapper.CollectSourceMapper;
import com.dcloud.manage.mapper.CollectSourceParamsDefineMapper;
import com.dcloud.manage.service.entity.CollectSource;
import com.dcloud.manage.service.entity.CollectSourceDefine;
import com.dcloud.manage.mapper.CollectSourceDefineMapper;
import com.dcloud.manage.service.ICollectSourceDefineService;
import com.dcloud.manage.service.entity.CollectSourceParamsDefine;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 数据源定义表
 * @Author: jeecg-boot
 * @Date:   2022-09-07
 * @Version: V1.0
 */
@Service
public class CollectSourceDefineServiceImpl extends ServiceImpl<CollectSourceDefineMapper, CollectSourceDefine> implements ICollectSourceDefineService {



    @Autowired
    private CollectSourceDefineMapper collectSourceDefineMapper;

    @Autowired
    private CollectSourceMapper collectSourceMapper;

    @Autowired
    private CollectSourceParamsDefineMapper collectSourceParamsDefineMapper;


    /**
     * @Author shijf
     * @Description 获取所有数据源类型
     * @Date 10:49 2022/10/14
     * @return java.util.List<com.dcloud.manage.service.entity.CollectSourceDefine>
     **/
    @Override
    public List<CollectSourceDefine> getAll() {

        List<CollectSourceDefine> collectSourceDefines = collectSourceDefineMapper.selectList(new LambdaQueryWrapper<>(CollectSourceDefine.class));
        return collectSourceDefines;
    }

    @Override
    public CollectSourceDefine selectBySourceCode(String sourceCode) {
        CollectSourceDefine collectSourceDefine = collectSourceDefineMapper.selectOne(new LambdaQueryWrapper<CollectSourceDefine>()
                .eq(CollectSourceDefine::getSourceCode, sourceCode));
        return collectSourceDefine;
    }

    @Override
    public void deleteById(Integer id) {



        List<CollectSource> collectSources = collectSourceMapper.selectList(new LambdaQueryWrapper<CollectSource>().eq(CollectSource::getSourceDefTid, id));
        if(collectSources.size() > 0){
            throw new BizException(BizErrorEnum.METADATA_ERROR_SOURCE_DEF_USED);
        }


        //删除数据源定义
        collectSourceDefineMapper.deleteById(id);

        //删除数据源定义关联的参数定义
        collectSourceParamsDefineMapper.delete(new LambdaQueryWrapper<CollectSourceParamsDefine>().eq(CollectSourceParamsDefine::getSourceTid, id));
    }
}
